﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Admin_qxgl : System.Web.UI.Page
{
    DataClassesDataContext db1 = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack == true)
        {
            Bindata();
            string IP = Request.ServerVariables.Get("Remote_Addr").ToString();
            string nowtime = DateTime.Now.ToString();
            DateTime nowtime1 = Convert.ToDateTime(nowtime);
            int nowusername = Convert.ToInt32(Session["username"]);
            var newrizhi = new rizhi
            {
                userName = nowusername,
                time = nowtime1,
                ip = IP,
                caozuo = "进入管理员信息管理"
            };
            db1.rizhi.InsertOnSubmit(newrizhi);
            db1.SubmitChanges();
        }
    }
    private void Bindata()
    {
        GridView2.DataSourceID = "";
        int userName = Convert.ToInt32(Session["username"].ToString());
        var AdminInfo = (from p in db1.adminInformation
                         where p.userName != 4 && p.userName != userName
                         select p).ToList();
        GridView2.DataSource = AdminInfo;
        GridView2.DataBind();
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lbl = (Label)e.Row.Cells[0].FindControl("Label2");
            lbl.Text = (GridView2.PageIndex * GridView2.PageSize + e.Row.RowIndex + 1).ToString();
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
        {
            System.Web.UI.WebControls.CheckBox checkbox1 = (System.Web.UI.WebControls.CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
            if (checkbox1.Checked == true)
            {
                string username = GridView2.DataKeys[i].Value.ToString();
                int username1 = Convert.ToInt32(username);
                var infomation = from r in db1.adminInformation where r.userName == username1 
                              select r;
                var Autority = from r in db1.AdminAutority
                               where r.userName == username1 
                                  select r;
                db1.adminInformation.DeleteAllOnSubmit(infomation);
                db1.AdminAutority.DeleteAllOnSubmit(Autority);
                db1.SubmitChanges();
                
            }
        }
        Response.Write("<script>alert('删除成功！')</script>");
        Bindata();
       
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("insert_dba.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int userName = Convert.ToInt32(Session["username"].ToString());
        var result = from c in db1.adminInformation
                     where c.tname.Contains(TextBox1.Text) && c.userName != 4 && c.userName != userName
                     select c;
        GridView2.DataSource = result;
        GridView2.DataBind();
       
    }
}